package com.sdp.irrigation.excel.field;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.irrigation.cache.IrrigationCleanCache;
import com.sdp.irrigation.db.irrigation_emp_field.service.IIrrigationEmpFieldService;
import com.sdp.irrigation.db.irrigation_emp_pump_well.service.IIrrigationEmpPumpWellService;
import com.sdp.irrigation.db.irrigation_pump_well.service.IIrrigationPumpWellService;
import com.sdp.irrigation.entity.IrrigationEmpField;
import com.sdp.irrigation.entity.IrrigationEmpPumpWell;
import com.sdp.irrigation.entity.IrrigationPumpWell;
import com.sdp.irrigation.view.EmpPumpWellRightExcelVo;
import com.sdp.right.entity.SysImportTemplateDetail;
import com.sdp.right.excel.importExcel.ImportExcelCallbackService;
import com.sdp.right.excel.importExcel.ImportExcelRowCallbackService;

@Service
public class EmpPumpWellExcelCallback implements ImportExcelCallbackService, ImportExcelRowCallbackService {

	private final ThreadLocal<EmpPumpWellRightExcelVo> threadLocal = ThreadLocal.withInitial(EmpPumpWellRightExcelVo::new);
	
	@Autowired
	private IIrrigationEmpFieldService empFieldService;
	
	@Autowired
	private IIrrigationEmpPumpWellService empPumpWellService;
	
	@Autowired
	private IIrrigationPumpWellService pumpWellService;
	
	@Autowired
	private IrrigationCleanCache irrigationCleanCache;
	
	@Override
	public <T> void execute(T rowData, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
//		IrrigationEmpPumpWell empPumpWell = (IrrigationEmpPumpWell) rowData;
//		Set<String> empNoSet = threadLocal.get();
//		empNoSet.add(empPumpWell.getEmpNo());
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> void executeBatch(List<T> rowDataList, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
		Set<String> empNoSet = threadLocal.get().getEmpNoSet();
		Set<String> deleteEmpNoSet = new HashSet<>();
		Set<String> tempPwNoSet = new HashSet<>();

		for (Object obj : rowDataList) {
			IrrigationEmpPumpWell row = (IrrigationEmpPumpWell) obj;
			tempPwNoSet.add(row.getPwNo());
			if (!empNoSet.contains(row.getEmpNo())) {
				deleteEmpNoSet.add(row.getEmpNo());
				empNoSet.add(row.getEmpNo());
			}
			row.setNodeKey(getPumpWellNodeKey(row.getPwNo()));
			row.setCreateTime(LocalDateTime.now());
			row.setUpdateTime(row.getCreateTime());
			//每次都重新删除插入，不再更新
			insertList.add((T) row);
		}
		
		if (deleteEmpNoSet.size() > 0) {
			//删除人员机井信息
			QueryWrapper<IrrigationEmpPumpWell> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().in(IrrigationEmpPumpWell::getEmpNo, deleteEmpNoSet);
			empPumpWellService.remove(queryWrapper);
			//删除人员区域信息
			QueryWrapper<IrrigationEmpField> fieldQueryWrapper = new QueryWrapper<>();
			fieldQueryWrapper.lambda().in(IrrigationEmpField::getEmpNo, deleteEmpNoSet);
			empFieldService.remove(fieldQueryWrapper);
			
		}
		
		//查询机井信息，处理人员区域信息
		QueryWrapper<IrrigationPumpWell> pumpWellQueryWrapper = new QueryWrapper<>();
		pumpWellQueryWrapper.lambda().in(IrrigationPumpWell::getPwNo, tempPwNoSet);
		List<IrrigationPumpWell> pumpWellList = pumpWellService.list(pumpWellQueryWrapper);
		Map<String, IrrigationPumpWell> pumpWellMap = new HashMap<>();
		for (IrrigationPumpWell irrigationPumpWell : pumpWellList) {
			pumpWellMap.put(irrigationPumpWell.getPwNo(), irrigationPumpWell);
		}
		
		Set<String> empFiledNoKeySet = threadLocal.get().getEmpNo2FiledNoSet();
		List<IrrigationEmpField> empFieldList = new ArrayList<>();
		for (Object obj : rowDataList) {
			IrrigationEmpPumpWell row = (IrrigationEmpPumpWell) obj;
			if (pumpWellMap.containsKey(row.getPwNo())) {
				String empNo2FiledNoKey = empNo2FiledNoKey(row.getEmpNo(), pumpWellMap.get(row.getPwNo()).getFieldNo());
				if (!empFiledNoKeySet.contains(empNo2FiledNoKey)) {
					empFiledNoKeySet.add(empNo2FiledNoKey);
					IrrigationEmpField empField = new IrrigationEmpField();
					empField.setEmpNo(row.getEmpNo());
					empField.setFieldNo(pumpWellMap.get(row.getPwNo()).getFieldNo());
					empField.setNodeKey(getFiledNodeKey(empField.getFieldNo()));
					empField.setProjectNo(row.getProjectNo());
					empField.setCreateTime(LocalDateTime.now());
					empField.setUpdateTime(empField.getCreateTime());
					empFieldList.add(empField);
				}
			}
		}
		
		if (empFieldList.size() > 0) {
			empFieldService.saveBatch(empFieldList);
		}
		
		//清理
		deleteEmpNoSet.clear();
		deleteEmpNoSet = null;
		tempPwNoSet.clear();
		tempPwNoSet = null;
		pumpWellList.clear();
		pumpWellList = null;
		pumpWellMap.clear();
		pumpWellMap = null;
		empFieldList.clear();
		empFieldList = null;
	}
	
	private String empNo2FiledNoKey(String empNo, String fieldNo) {
		return empNo + "#" + fieldNo;
	}
	
	private String getFiledNodeKey(String fieldNo) {
		return "field#" + fieldNo;
	}
	
	private String getPumpWellNodeKey(String pwNo) {
		return "pump#" + pwNo;
	}
	

	@Override
	public BusinessContext execute(HttpServletRequest request, HttpServletResponse response) {
		BusinessContext businessContext = new BusinessContext();
		irrigationCleanCache.cleanEmpPumpTreeAll();
		threadLocal.get().getEmpNo2FiledNoSet().clear();
		threadLocal.get().getEmpNoSet().clear();
		return businessContext;
	}

}
